//
//  ProfileViewController.m
//  欢乐美食
//
//  Created by 遇水 on 15/6/8.
//  Copyright (c) 2015年 遇水. All rights reserved.
//

#import "ProfileViewController.h"
#import "TableHeaderView.h"
#import "historyTool.h"
#import "HMMineArrowItem.h"
#import "HMMineGroup.h"
#import "HMMineCell.h"
#import "HMMineHeader.h"
#import "HMMineHeaderView.h"
#import "HMMineHeaderFrame.h"

@interface ProfileViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)  UITableView    *tableView;   //获得表格

@property (nonatomic, strong) TableHeaderView    *tableHeaderView;

@property (nonatomic, strong) NSMutableArray *datas; //组数据

@property (weak, nonatomic)  TableHeaderView *headerView;     //顶部控件

@property (nonatomic, strong) HMMineHeaderView *mineHeaderView;

@end

@implementation ProfileViewController

- (NSMutableArray *)datas
{
    if (_datas == nil) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addGroups];
    [self initHeaderView];
    [self initTableView];
}

- (void)addGroups
{
    // 第一组
    HMMineArrowItem *item01 = [[HMMineArrowItem alloc] initWithIcon:@"icon_mine_onsite" title:@"团购订单" subTitle:@"0" destClass:nil];
    
    HMMineArrowItem *item02 = [[HMMineArrowItem alloc] initWithIcon:@"icon_mine_myAccount_bankcard" title:@"外卖订单" subTitle:@"5" destClass:nil];
    
    HMMineArrowItem *item03 = [[HMMineArrowItem alloc] initWithIcon:@"icon_mine_onsite" title:@"预订订单" subTitle:@"10" destClass:nil];
    
    NSString *subTitle = [NSString stringWithFormat:@"%d",[historyTool sharedhistoryTool].history.count];
    HMMineArrowItem *item04 = [[HMMineArrowItem alloc] initWithIcon:@"icon_mine_onsite" title:@"浏览记录" subTitle:subTitle destClass:nil];
    
    HMMineGroup *group1 = [[HMMineGroup alloc] init];
    group1.items = @[item01, item02, item03, item04];
    
    // 第一组
    HMMineArrowItem *item11 = [[HMMineArrowItem alloc] initWithIcon:@"icon_mine_onsite" title:@"设置" subTitle:nil destClass:nil];
    HMMineGroup *group2 = [[HMMineGroup alloc] init];
    group2.items = @[item11];
    
    
    [self.datas addObject:group1];
    [self.datas addObject:group2];
}

/**
 初始化tableView
 */
-(void)initTableView{
    
//    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.headerView.height, self.view.width, self.view.height - self.headerView.height) style:UITableViewStyleGrouped];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStyleGrouped];

    tableView.delegate = self;
    tableView.dataSource = self;
    
    tableView.tableHeaderView = self.mineHeaderView;

    //设置没有分割线
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    self.tableView = tableView;

}

-(void)initHeaderView{
    
    HMMineHeaderView *mineHeaderView = [[HMMineHeaderView alloc]  init];
    HMMineHeader *mineHeader = [[HMMineHeader alloc] init];
    mineHeader.icon = @"icon_login_user";
    mineHeader.userName = @"张三";
    mineHeader.coinIcon = @"icon_myCoupon_coupon";
    mineHeader.coin = @"150";
    
    mineHeader.collectionIcon = @"icon_myCoupon_coupon";
    mineHeader.collection = @"80";
    
    HMMineHeaderFrame *headerFrame = [[HMMineHeaderFrame alloc] init];
    headerFrame.headerData = mineHeader;
    mineHeaderView.headerFrame = headerFrame;
    
    self.mineHeaderView = mineHeaderView;
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.datas.count;
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    if (section == 0) {
//        return self.mineHeaderView;
//    } else {
//        return nil;
//    }
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    HMMineGroup *group = self.datas[section];
    return group.items.count;
}


#pragma mark - table view delegate 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HMMineCell *cell = [HMMineCell cellWithTableView:tableView];
    HMMineGroup *group = self.datas[indexPath.section];
    cell.item = group.items[indexPath.row];
    
    if (indexPath.row == group.items.count - 1) {
        cell.hiddenSeperator = YES;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //选中
    if (indexPath.row == 3 ) {
        //跳转到另外一个控制器中，显示浏览记录
        
    }
}

@end
